package com.suncaper.han_yi_system.controller;

import com.suncaper.han_yi_system.pojo.entity.Role;
import com.suncaper.han_yi_system.result.Result;
import com.suncaper.han_yi_system.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Objects;

@RestController
@RequestMapping("/role")
@CrossOrigin(origins = "*")
@Tag(name = "角色管理")
@Slf4j
public class RoleController {

    @Autowired
    RoleService roleService;

    @Operation(summary = "新增角色")
    @PostMapping("/save")
    public Result save(@RequestBody Role role){
        role.setCreateTime(LocalDateTime.now());
        roleService.save(role);
        return Result.ok();
    }

    @Operation(summary = "更新角色")
    @PutMapping("/update")
    public Result update(@RequestBody Role role){
        role.setUpdateTime(LocalDateTime.now());
        roleService.updateById(role);
        return Result.ok();
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        roleService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id获取角色")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        return Result.ok().data("role",roleService.getById(id));
    }

    @Operation(summary = "获取角色列表")
    @RequestMapping("/list")
    public Result getList(){
        return Result.ok().data("roleList",roleService.list());
    }

}
